import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '@/views/HomeView.vue'
import Sidebar from '@/components/nav/NavComonents.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: '首页',
    meta: {
      title: "首页",
      roles: ['user', 'admin']
    },
    redirect: '/index',
    leaf: true, //是否为单节点，增加属性
    enabled: true,
    component: HomeView,
    icoClass: 'el-icon-s-home',
    children: [
      //管理首页
      {
        path: '/index',
        name: "管理首页",
        meta: {
          title: "管理首页",
          roles: ['user', 'admin']
        },
        leaf: true, //是否为单节点，增加属性
        enabled: true,
        icoClass: 'el-icon-s-home',
        components: {
          default: () => import('@/views/main/IndexView.vue'),
          sidebar: Sidebar
        }
      },
      //管理设置页面
      {
        path: '/setting',
        name: "个人信息",
        meta: {
          title: "个人信息",
          roles: ['user', 'admin']
        },
        components: {
          default: () => import('@/views/main/SetView.vue'),
          sidebar: Sidebar
        }
      },
      //管理测试页面1
      {
        path: '/pager1',
        name: "Pager1",
        meta: {
          title: "管理后台测试页面1",
          roles: ['admin']
        },
        components: {
          default: () => import('@/views/main/Pager1.vue'),
          sidebar: Sidebar
        }
      },
      //管理测试页面2
      {
        path: '/pager2',
        name: "Pager2",
        meta: {
          title: "管理后台测试页面2",
          roles: ['admin']
        },
        components: {
          default: () => import('@/views/main/Pager2.vue'),
          sidebar: Sidebar
        }
      },
      //交易市场
      {
        path: '/trademarket',
        name: "TradeMarketView",
        meta: {
          title: "交易市场",
          roles: ['user', 'admin']
        },
        components: {
          default: () => import('@/views/property/TradeMarketView.vue'),
          sidebar: Sidebar
        }
      },
      //交易记录
      {
        path: '/traderecord',
        name: 'traderecord',
        meta: {
          title: '交易记录',
          roles: ['user', 'admin']
        },
        components: {
          default: () => import('@/views/property/TradeRecordView.vue'),
          sidebar: Sidebar
        }
      },      
      //权限403
      {
        path: '/403',
        name: "403",
        meta: {
          title: "碳中和管理平台-403无权限",
          roles: ['user', 'admin']
        },
        components: {
          default: () => import('@/views/403.vue'),
          sidebar: Sidebar
        }
      },
    ]
  },
  //用户管理
  {
    path: '/user',
    name: '用户管理',
    meta: {
      title: "用户管理",
      roles: ['admin']
    },
    redirect: '/user/usermanager',
    leaf: true, //是否为单节点，增加属性
    enabled: true,
    component: HomeView,
    icoClass: 'el-icon-user',
    children: [
      //用户管理
      {
        path: '/user/usermanager',
        name: "后台用户管理",
        meta: {
          title: "用户管理",
          roles: ['admin']
        },
        leaf: true, //是否为单节点，增加属性
        enabled: true,
        icoClass: 'el-icon-user',
        components: {
          default: () => import('@/views/user/UserManagerView.vue'),
          sidebar: Sidebar
        }
      },
    ]
  },
  //账户管理
  {
    path: '/entermanager',
    name: '账户管理',
    meta: {
      title: "账户管理",
      roles: ['user', 'admin']
    },
    redirect: '/enter/entermanager',
    leaf: true, //是否为单节点，增加属性
    enabled: true,
    component: HomeView,
    icoClass: 'el-icon-bank-card',
    children: [
      //账户管理
      {
        path: '/entermanager',
        name: "后台账户管理",
        meta: {
          title: "账户管理",
          roles: ['user', 'admin']
        },
        leaf: true, //是否为单节点，增加属性
        enabled: true,
        icoClass: 'el-icon-bank-card',
        components: {
          default: () => import('@/views/enterprise/EnterManagerView.vue'),
          sidebar: Sidebar
        }
      },
    ]
  },
  //账户审核
  {
    path: '/account/',
    name: '账户审核',
    meta: {
      title: "账户审核",
      roles: ['admin']
    },
    redirect: '/account/checkenter',
    leaf: true, //是否为单节点，增加属性
    enabled: true,
    component: HomeView,
    icoClass: 'el-icon-document-checked',
    children: [
      //账户审核
      {
        path: '/account/checkenter',
        name: "后台账户审核",
        meta: {
          title: "账户审核",
          roles: ['admin']
        },
        leaf: true, //是否为单节点，增加属性
        enabled: true,
        icoClass: 'el-icon-document-checked',
        components: {
          default: () => import('@/views/enterprise/CheckEnterpriseView.vue'),
          sidebar: Sidebar
        }
      },
    ]
  },
  //资产管理
  {
    path: '/propmanager',
    redirect: '/index',
    name: "资产管理",
    meta: {
      title: "资产管理",
      roles: ['user', 'admin']
    },
    leaf: false, //是否为单节点，增加属性
    enabled: true, //启用
    icoClass: 'el-icon-s-marketing',
    components: {
      default: HomeView,
      sidebar: Sidebar
    },
    children: [
      //资产管理
      {
        path: '/propmanager',
        name: "碳信用",
        meta: {
          title: "资产管理",
          roles: ['user', 'admin']
        },
        enabled: true,
        components: {
          default: () => import('@/views/property/PropertyView.vue'),
          sidebar: Sidebar
        },
      },
      //交易市场
      {
        path: '/trademarket',
        name: "交易市场",
        meta: {
          title: "交易市场",
          roles: ['user', 'admin']
        },
        enabled: true,
        components: {
          default: () => import('@/views/property/TradeMarketView.vue'),
          sidebar: Sidebar
        }
      },
      //交易记录
      {
        path: '/traderecord',
        name: '交易记录',
        meta: {
          title: '交易记录',
          roles: ['user', 'admin']
        },
        enabled: true,
        components: {
          default: () => import('@/views/property/TradeRecordView.vue'),
          sidebar: Sidebar
        }
      },
    ]
  },
  //数据统计
  {
    path: '/data/',
    name: '数据统计',
    meta: {
      title: "数据统计",
      roles: ['admin']
    },
    redirect: '/data/analysis',
    leaf: true, //是否为单节点，增加属性
    enabled: true,
    component: HomeView,
    icoClass: 'el-icon-s-data',
    children: [
      //数据统计
      {
        path: '/data/analysis',
        name: "数据统计",
        meta: {
          title: "数据统计",
          roles: ['admin']
        },
        leaf: true, //是否为单节点，增加属性
        enabled: true,
        icoClass: 'el-icon-s-data',
        components: {
          default: () => import('@/views/data/DataView.vue'),
          sidebar: Sidebar
        }
      },
    ]
  },
  {
    path: '/login',
    name: 'LoginView',
    meta: {
      title: "碳中和管理平台-登录"
    },
    enabled: false,
    component: () => import('@/views/LoginView.vue')
  },
  {
    path: '/reg',
    name: 'RegView',
    meta: {
      title: "碳中和管理平台-注册"
    },
    enabled: false,
    component: () => import('@/views/RegView.vue')
  }
]

//将路由表信息存放到store中,注意变量名称不要写错了
localStorage.setItem('route', JSON.stringify(routes));

const router = new VueRouter({
  routes
})

//路由验证登录
router.beforeEach(function (to, from, next) {

  var user = localStorage.getItem("token");
  let user_role = '';
  //先给user_role一个初始值，然后检查vuex中是否有用户信息，再进行赋值，以进行路由访问权限判定
  if (localStorage.getItem('User')) {
    user_role = JSON.parse(localStorage.getItem('User')).user_role ? JSON.parse(localStorage.getItem('User')).user_role : 'user';
  }


  if (to.path == '/reg' || to.path == '/login') {
    next()
    return
  }

  if (user != null || to.path === '/login') {
    if (to.meta.roles && !to.meta.roles.includes(user_role) && to.path !== '/login' && to.path !== '/reg') {
      //无权限，重定向到首页或者其他无权限页面
      next('/403')
    } else {
      //有权限，正常导航
      next();
    }
  } else {
    if (user === null || user === '') {
      next('/login');
    }
  }
});

export default router
